<template>
  <div class="code-card">
    <button class="halo-close-btn" @click="$emit('close')">
      <i class="iconfont icon-close"></i>
    </button>
    <div class="title">请输入验证码</div>
    <div class="subtitle">验证码已发送到 {{ phone }}</div>
    <div class="input-box">
      <div
        v-for="(item, key) in nums"
        :key="key"
        class="num"
        :class="{ active: isFocus && key === code.length }"
      >
        {{ item }}
      </div>
      <input
        ref="input"
        v-model="code"
        class="input"
        type="number"
        pattern="\d"
        @input="changeCode"
        @focus="isFocus = true"
        @blur="isFocus = false"
      />
    </div>
    <div class="time" v-if="num !== 0">{{ num }}s后可重新发送</div>
    <div class="time resend" :style="{ color }" v-else>重新发送验证码</div>
  </div>
</template>

<script>
export default {
  props: {
    phone: {
      type: [Number, String],
    },
    color: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      isFocus: false,
      code: '',
      num: Math.max(60 - ~~((new Date().getTime() - (window.lastfetchCodeTime || 0)) / 1000), 0),
    };
  },
  computed: {
    nums() {
      return this.code.split('').concat(['', '', '', '']).slice(0, 4);
    },
  },
  mounted() {
    this.startTimer();
    this.$refs.input.focus();
  },
  beforeUnmount() {
    this.stopTimer();
  },
  methods: {
    restart() {
      this.num = 60;
      this.startTimer();
    },
    startTimer() {
      this.stopTimer();
      this.timer = setInterval(() => {
        this.num -= 1;
        if (this.num <= 0) this.stopTimer();
      }, 1000);
    },
    stopTimer() {
      if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
      }
    },
    resend() {
      this.$emit('send');
      this.restart();
    },
    changeCode() {
      const {input} = this.$refs
      if (!input) return
      this.code = input.value = input.value.replace(/\D/g, '')
      if (this.code.length !== 4) return;
      this.$emit('success', this.code);
    },
  },
};
</script>

<style lang="less" scoped>
.code-card {
  width: 610px;
  background: #ffffff;
  border-radius: 12px;
  text-align: center;
}
.halo-close-btn {
  position: absolute;
  top: 0;
  right: 0;
  transform-origin: top right;
  transform: scale(0.9);
}
.title {
  font-size: 34px;
  color: #323038;
  line-height: 48px;
  padding: 40px 0 12px;
  font-weight: bold;
}
.subtitle {
  font-size: 28px;
  color: #95949d;
  line-height: 44px;
  padding-bottom: 35px;
}
.time {
  font-size: 28px;
  color: #95949d;
  line-height: 44px;
  padding: 48px 0 30px;
  &.resend {
    color: #2978ff;
    font-weight: bold;
  }
}
.input-box {
  position: relative;
  width: 482px;
  height: 90px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .num {
    margin: 0 15px;
    flex: 1;
    height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-bottom: 2px solid #cccccc;
    font-weight: bold;
    font-size: 38px;
    &.active::after {
      content: '';
      display: block;
      width: 2px;
      height: 40px;
      background-color: #2978ff;
      animation: blink 1s infinite steps(1, start);
    }
  }
  .input {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }
}

@keyframes blink {
  0% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}
</style>